package cn.bellychang.stack.liuxin;

import cn.bellychang.arraylist.liuxin.ArrayList;
import cn.bellychang.iiterface.bobo.Stack;

/**
 * LIFO
 * array[getSize] in  array[getSize] out
 *
 * @author: bellychang
 * @date: 2020/2/7
 */
public class ArrayStack<E> implements Stack<E> {

    private ArrayList<E> array = new ArrayList(1);

    /**
     * add last
     * @param e
     */
    @Override
    public void push(E e) {
        array.addLast(e);
    }

    /**
     * pop last
     * @return
     */
    @Override
    public E pop() {
        return array.remove(array.getSize()-1);
    }

    @Override
    public E peek() {
        return array.get(array.getSize() - 1);
    }

    @Override
    public boolean isEmpty() {
        return array.getSize() == 0;
    }

    @Override
    public int size() {
        return array.getSize();
    }

    @Override
    public String toString() {
        return "ArrayStack Bottom{" + array + '}';
    }
}
